<template>
	<view class="content">
		<image class="logo" src="../../static/imgs/logo.png" mode=""></image>
		<view class="form">
			<view v-if='type==1'>
				<view class=" list">
					<image class="icon" src="../../static/imgs/icon_username.png" mode=""></image>
					<text class="title">账号</text>
					<p class='inputBox'><input class="m-input" type="text" clearable focus v-model="account" placeholder="请输入账号"></input></p>

				</view>
				<view class="list">
					<image class="icon" src="../../static/imgs/icon_password.png" mode=""></image>
					<text class="title">密码</text>
					<p class='inputBox'><input type="password" displayable v-model="password" placeholder="请输入密码"></input></p>

				</view>
				<navigator class="pwd" url="../pwd/pwd">忘记密码</navigator>
			</view>
			<view v-if='type==2'>
				<view class=" list">
					<image class="icon" src="../../static/imgs/ic_account_phone.png" mode=""></image>
					<text class="title">手机号</text>
					<p class='inputBox'><input class="m-input" type="text" clearable focus v-model="phone" @input="getphone" placeholder="请输入你的手机号"></input></p>

				</view>
				<!-- <view class="list">
					<image class="icon" src="../../static/imgs/ic_account_code.png" mode=""></image>
					<text class="title">验证码</text>
					<p class='inputBox'>
						<input type="text" displayable v-model="code" placeholder="请输入验证码"></input>
						
					</p>

				</view> -->
				<navigator v-if="isphone==false" class="pwd" url="../pwd/pwd">忘记密码</navigator>
			</view>

			
		</view>
		<view class="btn-row">
			<button type="primary" class="primary"  :class="[isphone ? 'ee' : '' ]" @tap="getCode" v-if='type==2'>获取验证码</button>
			<button type="primary" class="primary" @tap="bindLogin" v-if='type==1'>{{islogin}}</button>
		</view>
		<view class="text-center">
			<p class="color_9 font_12" @tap=getType>{{loginaname}}</p>
			<p class="color_9 font_12 mg_top70">其他方式登录</p>
			<image class="wechat" src="../../static/imgs/ic_login_wechat.png" mode=""></image>


		</view>
		<view class="checkedBox">
			<view>
				<label class="radio">
					<radio value="r1" checked="true" class="checked" style="" />
					我已阅读并同意
					<navigator url="">
						《找房找家服务协议》
					</navigator>
					和
					<navigator url="">
						《隐私政策》
					</navigator>
				</label>

			</view>
		</view>
		<view class="oauth-row" v-if="hasProvider" v-bind:style="{top: positionTop + 'px'}">
			<view class="oauth-image" v-for="provider in providerList" :key="provider.value">
				<image :src="provider.image" @tap="oauth(provider.value)"></image>
				<!-- #ifdef MP-WEIXIN -->
				<button v-if="!isDevtools" open-type="getUserInfo" @getuserinfo="getUserInfo"></button>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	import service from '../../service.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import mInput from '../../components/m-input.vue'

	export default {
		components: {
			mInput
		},
		data() {
			return {
				type: 1, //1 账号密码登录 2 手机号登录
				islogin: '登录',
				loginaname: '手机验证登录',
				providerList: [],
				hasProvider: false,
				account: '',
				password: '',
				phone:"",
				positionTop: 0,
				isDevtools: false,
				code: '',
				countdown:'获取验证码',
				  disabled:false,
				timestatus:false,
				isphone:true,
				clear:'',
			}
		},
		computed: mapState(['forcedLogin']),
		methods: {
			...mapMutations(['login']),
			initProvider() {
				const filters = ['weixin', 'qq', 'sinaweibo'];
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						if (res.provider && res.provider.length) {
							for (let i = 0; i < res.provider.length; i++) {
								if (~filters.indexOf(res.provider[i])) {
									this.providerList.push({
										value: res.provider[i],
										image: '../../static/img/' + res.provider[i] + '.png'
									});
								}
							}
							this.hasProvider = true;
						}
					},
					fail: (err) => {
						console.error('获取服务供应商失败：' + JSON.stringify(err));
					}
				});
			},
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			getType() {
				if (this.type == 1) {
					this.type = 2
					this.loginaname = '账号密码登录'
					console.log(this.type)
				} else if (this.type == 2) {
					this.type = 1
					this.loginaname = '手机码登录'
				}

			},
			getphone(e){
			 console.log(e.target.value)	
				if(e.target.value==''){
					this.isphone=true
				}else{
					this.isphone=false
				}
			},
			//验证码跳转页面
			getCode(){
				if(this.phone){
					uni.navigateTo({
						url: 'getcode??type='+1+'&phone='+this.phone
						
					})
				}else{
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号'
					});
				}
				},
			
			
			bindLogin() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const data = {
					account: this.account,
					password: this.password
				};
				const validUser = service.getUsers().some(function(user) {
					return data.account === user.account && data.password === user.password;
				});
				if (validUser) {
					this.toMain(this.account);
				} else {
					uni.showToast({
						icon: 'none',
						title: '用户账号或密码不正确',
					});
				}
			},
			oauth(value) {
				uni.login({
					provider: value,
					success: (res) => {
						uni.getUserInfo({
							provider: value,
							success: (infoRes) => {
								/**
								 * 实际开发中，获取用户信息后，需要将信息上报至服务端。
								 * 服务端可以用 userInfo.openId 作为用户的唯一标识新增或绑定用户信息。
								 */
								this.toMain(infoRes.userInfo.nickName);
							},
							fail() {
								uni.showToast({
									icon: 'none',
									title: '登陆失败'
								});
							}
						});
					},
					fail: (err) => {
						console.error('授权登录失败：' + JSON.stringify(err));
					}
				});
			},
			getUserInfo({
				detail
			}) {
				if (detail.userInfo) {
					this.toMain(detail.userInfo.nickName);
				} else {
					uni.showToast({
						icon: 'none',
						title: '登陆失败'
					});
				}
			},
			toMain(userName) {
				this.login(userName);
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					uni.navigateBack();
				}

			}
		},
		onReady() {
			this.initPosition();
			this.initProvider();
			// #ifdef MP-WEIXIN
			this.isDevtools = uni.getSystemInfoSync().platform === 'devtools';
			// #endif
		}
	}
</script>

<style scoped='scoped'>
	.content {
		background: #fff;
		padding: 0 75upx;
	}

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}



	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		margin: 0 20px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}

	.logo {
		width: 110upx;
		height: 110upx;
		margin: 50upx auto;
	}

	.icon {
		width: 36upx;
		height: 38upx;
		vertical-align: middle;
		margin-right: 11upx;
	}

	.title {
		font-size: 26upx;
		color: #222;
	}

	.list {
		padding: 24upx 0;
		border-bottom: 1px solid #CCCCCC;
	}

	.inputBox {
		margin-top: 40upx;
		display: flex;
		position: relative;
		
	}
 .get-vcode{
        position: absolute;
        right: 5%;
        top: 15upx;
        height: 60upx;
        line-height: 60upx;
        font-size: 30upx;
        color: #FFFFFF;
        background: #0779B5;
        border-radius: 50upx;
        border: 1upx solid #CCCCCC;
    }
	.inputBox input {
		font-size: 26upx;
		color: #222;
	}

	button.primary {
		width: 520upx;
		height: 72upx;
		line-height: 72upx;
		font-size: 34upx;
		background: linear-gradient(0deg, rgba(255, 26, 118, 1) 0%, rgba(255, 123, 41, 1) 100%);
		border-radius: 16upx;
	}
	button.primary.ee {
		width: 520upx;
		height: 72upx;
		line-height: 72upx;
		font-size: 34upx;
		background: #CCCCCC!important;
		border-radius: 16upx;
	}

	.pwd {
		text-align: right;
		color: #999999;
		font-size: 24upx;
		line-height: 60upx;
	}

	.wechat {
		width: 80upx;
		height: 80upx;
		margin-top: 20upx;
	}

	.checkedBox {
		display: flex;
		font-size: 22upx;
		color: #666666;
	}

	.checkedBox navigator {
		display: inline-block;
		color: #4F9EFF;
	}

	.checkedBox {
		margin-top: 50upx;
	}

	 
</style>
